<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class cart_uqcms extends control
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    function settle()
    {
        if (cfg('sys_settle_login') == '1') {
            if (empty($this->uid)) {
                showmsg('未登录，请先登录。', 'index.php?m=cart&a=index');
            }
        }
        if ($_POST) {
            $this->display();
        } else {
            showmsg('提交数据错误。', 'index.php?m=cart&a=index');
        }
    }
    function create_order()
    {
        if (cfg('sys_settle_login') == '1') {
            if (empty($this->uid)) {
                error_json('超时，刷新页面，重新登录。');
            }
        }
        $arr['address_id'] = !empty($_POST['address_id']) ? $_POST['address_id'] : error_json('地址不能为空');
        $arr['message'] = !empty($_POST['message']) ? $_POST['message'] : '';
        $settle_result = $this->module('cart')->settle($arr);
        if (isset($settle_result)) {
            $result = $this->module('cart')->create_order($settle_result, $arr);
            if ($result['total_price'] > '0') {
                $arr['trade_no'] = $result['trade_no'];
                right_json($arr);
            } else {
                error_json('订单提交出错，请重新下单。');
            }
        } else {
            error_json('订单出错，请重新下单。');
        }
    }
    function get_express_price()
    {
        $area_id = isset($_GET['area']) ? trim($_GET['area']) : error_json('地址不能为空');
        $post_order = isset($_SESSION['post_order']) ? $_SESSION['post_order'] : error_json('订单不能为空');
        $cart_in = implode(',', $post_order);
        $cquery = $this->db->get_all("select * from " . table('cart') . " where id in(" . $cart_in . ")");
        if ($cquery) {
            foreach ($cquery as $v) {
                $shop_arr[$v['shop_id']][] = $v;
                $goods_arr[] = $v['gid'];
            }
            $goods_in = implode(',', $goods_arr);
            $glist = $this->db->get_all("select id,express from " . table('goods') . " where id in(" . $goods_in . ")");
            if ($glist) {
                foreach ($glist as $v) {
                    $tpl_arr[] = $v['express'];
                }
                $epr_result = $this->module('express')->get_price($tpl_arr, $area_id);
                $result['error'] = '0';
                $result['list'] = $epr_result;
                echo json_encode($result);
            }
        }
    }
    public function check_pay_status()
    {
        $trade_no = $_GET['trade_no'] ? $_GET['trade_no'] : exit('error');
        if (isset($this->uid)) {
            $row = $this->db->get_one("select * from " . table("admin_pay") . " where uid = " . $this->uid . " and trade_no = '" . $trade_no . "'");
            if ($row['pay_status']) {
                $arr['pay_type'] = $row['pay_type'];
                right_json($arr);
            } else {
                error_json('产品未支付');
            }
        } else {
            error_json('未登录');
        }
    }
    public function add()
    {
        $gid = isset($_POST['gid']) ? $_POST['gid'] : error_json('没有选择产品');
        $sku_id = isset($_POST['sku_id']) ? intval($_POST['sku_id']) : '';
        $shop_id = isset($_POST['shop_id']) ? $_POST['shop_id'] : error_json('店铺选择不能为空');
        $buy_num = isset($_POST['buy_num']) ? intval($_POST['buy_num']) : '1';
        if (empty($sku_id)) {
            exit(json_encode(array('error' => '2', 'msg' => '请选择商品规格')));
        } else {
            $wsql = " and id = " . $sku_id;
            $row = $this->db->get_one("select id,gid,id,value,stock from " . table('goods_sku_index') . " where gid = " . $gid . $wsql . ' limit 1');
            if ($row['id']) {
                if ($row['stock'] == '0') {
                    exit(json_encode(array('error' => '5', 'msg' => '库存不足')));
                } else {
                    $cart_id = $this->func_add($row['gid'], $shop_id, $buy_num, $row['id']);
                    exit(json_encode(array('error' => '0', 'cart_id' => $cart_id)));
                }
            } else {
                exit(json_encode(array('error' => '2', 'msg' => '请选择商品规格')));
            }
        }
    }
    private function func_add($gid, $shop_id, $buy_num, $sku_id)
    {
        if (isset($_COOKIE['goods']['cart'])) {
            $cookie = $_COOKIE['goods']['cart'];
        } else {
            $cookie = $this->cookie_add();
        }
        if (isset($this->uid)) {
            $wsql = " uid = " . $this->uid;
        } else {
            $wsql = " cookie = '" . $cookie . "' ";
        }
        $crow = $this->db->get_one("select * from " . table('cart') . " where sku_id = '" . $sku_id . "' and " . $wsql);
        if ($crow['id']) {
            $this->db->query("update " . table('cart') . " set buy_num=" . $buy_num . " where id = " . $crow['id']);
            $cart_id = $crow['id'];
            return $cart_id;
        } else {
            if (isset($this->uid)) {
                $setarr['uid'] = $this->uid;
                $setarr['cookie'] = '';
            } else {
                $setarr['uid'] = '0';
                $setarr['cookie'] = $cookie;
            }
            $setarr['shop_id'] = $shop_id;
            $setarr['gid'] = $gid;
            $setarr['sku_id'] = $sku_id;
            $setarr['buy_num'] = $buy_num;
            $setarr['addtime'] = time();
            $query = $this->db->add(table('cart'), $setarr);
            if ($query) {
                $cart_id = $this->db->insert_id();
                return $cart_id;
            } else {
                return false;
            }
        }
    }
    private function cookie_add()
    {
        $computer_cookie = build_order_no();
        $time = time() + 3200 * 24 * 365;
        setcookie("goods[cart]", $computer_cookie, $time);
        return $computer_cookie;
    }
    public function del()
    {
        if (isset($this->uid)) {
            $wsql = " and uid = " . $this->uid;
        } else {
            $userCookie = $_COOKIE['goods']['cart'];
            $wsql = " and cookie ='" . $userCookie . "'";
        }
        $cartid = isset($_POST['cartid']) ? intval($_POST['cartid']) : error_json('提交编号错误');
        $wsql = "id = " . $cartid . $wsql;
        $query = $this->db->delete(table('cart'), $wsql);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    public function del_all()
    {
        $spid = isset($_POST['spid']) ? $_POST['spid'] : false;
        $wsql = '';
        if (isset($this->uid)) {
            $wsql .= ' uid = ' . $this->uid . '  and ';
        } else {
            if (isset($_COOKIE['goods']['cart'])) {
                $cookie = $_COOKIE['goods']['cart'];
            } else {
                $cookie = $this->cookie_add();
            }
            $wsql .= " cookie = '" . $cookie . "' and ";
        }
        if ($spid) {
            $wsql .= ' shop_id = ' . $spid . ' and ';
        }
        $wsql = trim($wsql, ' and ');
        $query = $this->db->delete(table('cart'), $wsql);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    public function num()
    {
        if (isset($this->uid)) {
            $wsql = " uid = " . $this->uid;
        } else {
            if (isset($_COOKIE['goods']['cart'])) {
                $cookie = $_COOKIE['goods']['cart'];
            } else {
                $cookie = $this->cookie_add();
            }
            $wsql = " cookie = '" . $cookie . "' ";
        }
        $wsql = ' where ' . $wsql;
        $row = $this->db->get_one("select sum(buy_num) as total from " . table('cart') . $wsql);
        if ($row['total']) {
            $arr['num'] = $row['total'];
            right_json($arr);
        } else {
            error_json();
        }
    }
    public function edit_num()
    {
        $cartid = isset($_POST['cartid']) ? $_POST['cartid'] : error_json('提交编号错误');
        $buy_num = isset($_POST['num']) ? $_POST['num'] : '1';
        if (isset($this->uid)) {
            $wsql = " and uid = " . $this->uid;
        } else {
            if (isset($_COOKIE['goods']['cart'])) {
                $cookie = $_COOKIE['goods']['cart'];
            } else {
                $cookie = $this->cookie_add();
            }
            $wsql = " and cookie ='" . $cookie . "'";
        }
        $wsql = ' where id = ' . $cartid . $wsql;
        $query = $this->db->query("update " . table('cart') . " set buy_num= " . $buy_num . $wsql);
        if ($query) {
            right_json();
        } else {
            error_json();
        }
    }
    public function small_list()
    {
        $this->module('cart')->update_cart();
        if (!empty($this->uid)) {
            $wsql = " where uid = " . $this->uid;
        } else {
            $ucookie = $_COOKIE['goods']['cart'];
            $wsql = " where cookie = '" . $ucookie . "'";
        }
        $query = $this->db->get_all("select * from " . table('cart') . $wsql);
        if ($query) {
            foreach ($query as $v) {
                $cartid[$v['sku_id']] = $v['id'];
                $buy_num[$v['sku_id']] = $v['buy_num'];
                $sku_arr[] = $v['sku_id'];
            }
            $img_size = img_size('80x80');
            if (isset($sku_arr)) {
                $sku_id_in = implode(',', $sku_arr);
                $sku_query = $this->db->get_all("select * from " . table('goods_sku_index') . " where id in(" . $sku_id_in . ")");
                if ($sku_query) {
                    foreach ($sku_query as $v) {
                        $gid_arr[] = $v['gid'];
                        $sku_data[] = $v;
                    }
                }
                if (isset($sku_data)) {
                    $gid_in = implode(',', $gid_arr);
                    $gquery = $this->db->get_all("select id,title,price,images,stock from " . table('goods') . " where id in (" . $gid_in . ")");
                    if ($gquery) {
                        foreach ($gquery as $v) {
                            $goods[$v['id']] = $v;
                        }
                    }
                    $total_price = 0;
                    $sku_img_arr = $this->module('goods_sku')->get_sku_img($gid_arr);
                    $sec_price_arr = $this->module('goods_act')->seckill_price($gid_arr);
                    $total_num = 0;
                    foreach ($sku_data as $v) {
                        $cinfo['id'] = $cartid[$v['id']];
                        $cinfo['gid'] = $goods[$v['gid']]['id'];
                        $cinfo['title'] = $goods[$v['gid']]['title'];
                        if (!empty($v['value'])) {
                            $val_arr = explode(',', $v['value']);
                            foreach ($val_arr as $vv) {
                                if (!empty($sku_img_arr[$v['gid']][$vv]['images'])) {
                                    $cinfo['images'] = $sku_img_arr[$v['gid']][$vv]['images'] . $img_size;
                                    $img_size = '';
                                    break;
                                }
                            }
                        }
                        if (empty($cinfo['images'])) {
                            $cinfo['images'] = $goods[$v['gid']]['images'] . $img_size;
                            $img_size = '';
                        }
                        if (isset($sec_price_arr[$v['id']])) {
                            $cinfo['price'] = $sec_price_arr[$v['id']];
                        } else {
                            $cinfo['price'] = $v['price'];
                        }
                        $cinfo['buy_num'] = $buy_num[$v['id']];
                        $total_num += $buy_num[$v['id']];
                        $total_price += $cinfo['price'];
                        $clist[] = $cinfo;
                        unset($cinfo);
                    }
                    $arr['list'] = $clist;
                    $arr['total_price'] = $total_price;
                    $arr['total_num'] = $total_num;
                    right_json($arr);
                } else {
                    error_json('购物车为空');
                }
            } else {
                error_json('购物车为空');
            }
        } else {
            error_json('购物车为空');
        }
    }
}